package org.iteam.xlz.manager.action.membershipcard;

import java.math.BigDecimal;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.membershipcard.MembershipCard;
import org.iteam.xlz.core.entity.user.UserInfo;
import org.iteam.xlz.core.service.membershipcard.CardService;
import org.iteam.xlz.core.service.membershipcard.CardTypeService;
import org.iteam.xlz.core.service.user.UserInfoService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/membershipcard/card/index.html"),
    @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/membershipcard/card/edit.html")})
public class CardAction extends AbstractModuleAction<MembershipCard> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4603285546897591101L;
	private final String PATH="/membershipcard/card";
	
	@Override
	public MembershipCard getModel() {
		if(null==model){
			model=new MembershipCard();
		}
		return model;
	}
	@Override
	@Action(PATH+"/index")
	public String index() {
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",CardService.getInstance().counts(model.getUid()));
		map.put("rows",CardService.getInstance().query(model.getUid(),page,rows));
		return MAP;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		map.put("state",CardService.getInstance().create(model));
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		map.put("state",CardService.getInstance().update(model,recharge));
		return MAP;
	}

	public String delete() {
		return null;
	}

	@Override
	@Action(PATH+"/search")
	public String destroy() {
		UserInfo u=UserInfoService.getInstance().findByMobile(mobile);
		map.put("state",null==u?0:u.getUid()+"_"+u.getUsername());
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() {
		super.object=CardService.getInstance().find(_id);
		MembershipCard m=(MembershipCard)super.object;
		map.put("typeTree",CardTypeService.getInstance().getTypeTree());
		map.put("typeparent",CardTypeService.getInstance().getTypeParent(m.getMtid()));
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() {
		map.put("typeTree",CardTypeService.getInstance().getTypeTree());
		return EDIT;
	}
	@Override
	public String show() {
		return null;
	}

	
	private BigDecimal recharge=null;

	private String mobile;

	public void setRecharge(BigDecimal recharge) {
		recharge.setScale(2);
		this.recharge = recharge;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
}
